﻿using CodeCity.SDK;
using CodeCity.UserManagement.Model;
using CodeCity.UserManagement.Service.ApplicationModel;
using CodeCity.UserMangament.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeCity.UserManagement.Service
{
    public class HosiptalService
    {
        private readonly HosiptalRepository hosiptalRepository;

        public HosiptalService(HosiptalRepository hosiptalRepository)
        {
            this.hosiptalRepository = hosiptalRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task RegisterAsync(HosiptalRegisterModel model)
        {
            if (await hosiptalRepository.ExistAsync(model.Id))
            {
                throw new OperationException("该医院已注册");
            }
            hosiptalRepository.Insert(new Hosiptal()
            {
                BuildingTime = model.CreationTime,
                Patients = model.Patients ?? new long[0],
                MaxPregnants = model.MaxPregnants,
                UnitId = model.Id,
                MaxPatients = model.MaxPatients,
                MaxWorkers = model.MaxWorkers,
                Position = model.Position,
                Workers = model.Workers ?? new long[0],
                IsUnderConstruction = model.IsUnderConstruction,
                Pregnants = model.Pregnants ?? new long[0]
            });
            await hosiptalRepository.SaveChangesAsync();
        }

        public async Task ResignWorkAsync(long hosiptalId, long humanId)
        {
            var hosiptal = await hosiptalRepository.GetByUnitIdAsync(hosiptalId);
            if (hosiptal == null)
            {
                throw new OperationException("医院不存在");
            }

            var existWorkers = hosiptal.Workers.ToList();
            existWorkers.Remove(humanId);
            hosiptal.Workers = existWorkers.ToArray();
            await hosiptalRepository.SaveChangesAsync();
        }

        public async Task CompleteAsync(BuildingCompleteModel model)
        {
            var hosiptal = await hosiptalRepository.GetByUnitIdAsync(model.Id);
            if (hosiptal == null)
            {
                throw new OperationException("医院不存在");
            }
            hosiptal.IsUnderConstruction = false;
            hosiptal.CompleteTime = model.CompleteTime;
            await hosiptalRepository.SaveChangesAsync();
        }

        public async Task BeginWorkAsync(long buildingId, long humanId)
        {
            var hosiptal = await hosiptalRepository.GetByUnitIdAsync(buildingId);
            if (hosiptal == null)
            {
                throw new OperationException("医院不存在");
            }

            var existWorkers = hosiptal.Workers.ToList();
            if (existWorkers.Contains(humanId))
            {
                return;
            }
            existWorkers.Add(humanId);
            hosiptal.Workers = existWorkers.ToArray();
            await hosiptalRepository.SaveChangesAsync();
        }

        public Task<List<Hosiptal>> LoadAllAsync()
        {
            return hosiptalRepository.GetAllAsync();
        }
    }
}
